package com.damei.qmui_test_demo.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.damei.qmui_test_demo.R;
import com.damei.qmui_test_demo.activity.home.DataListActivity;
import com.damei.qmui_test_demo.model.HomeResponse;
import com.damei.qmui_test_demo.view.HomeListAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页
 */
public class HomeActivity extends Fragment {

    @BindView(R.id.data_list_view)
    ListView listView;

    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_home, container, false);
        ButterKnife.bind(this, view);
        initRecycerView();
        return view;
    }

    /**
     * 初始化加载
     */
    private void initRecycerView() {
        HomeListAdapter adapter = new HomeListAdapter(getActivity(), R.layout.home_list_item, getData());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this::onListClick);

        refreshLayout.setPrimaryColors(Color.parseColor("#00F63A52"));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("HomeActivity----------", "3");
    }

    /**
     * 初始化列表数据
     * @return
     */
    private List<HomeResponse> getData() {
        List<HomeResponse> list= new ArrayList<HomeResponse>();
        for(int i=0;i<5;i++)
        {
            HomeResponse model = new HomeResponse();
            model.setTodayNum(8);
            model.setYesTodayNum(12);
            model.setAllNum(100);
            model.setMountNum(20);
            model.setWeekNum(13);
            list.add(model);
        }
        return list;
    }

    /**
     * 列表点击事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    private void onListClick(AdapterView<?> parent, View view, int position, long id) {
        Log.e("flytosky","信息展示：我点击的是—— "+position);

        Intent intent = new Intent(getActivity(), DataListActivity.class);
        intent.putExtra("type", position);
        startActivity(intent);
    }
}
